<?php 
   session_start();

   include '../conexao.php';

   $tipo = $_GET['tipo'];

   $sWhere  = "Where      Pac.Id_Paciente = Des.Paciente_Id_Paciente ";
   $sWhere .= "           And Est.Id_Estabelecimento = Des.Estabelecimento_Id_Estabelecimento ";
   $sWhere .= "           And Tde.Id_Tipo_Despesa = Des.Tipo_Despesa_Id_Tipo_Despesa ";

   if($_GET['idPac'] != null) {
      $sWhere .= "          And Pac.Id_Paciente = " . $_GET['idPac'] . " ";
   }

   if($_GET['idEstab'] != null) {
      $sWhere .= "          And Est.Id_Estabelecimento = " . $_GET['idEstab'] . " ";
   }
    
   if($_GET['idTipoDesp'] != null) {
      $sWhere .= "          And Tde.Id_Tipo_Despesa = " . $_GET['idTipoDesp'] . " ";
   }

   if($_GET['dataIni'] != null) {
      $sWhere .= "          And Str_To_Date(Des.Data_Desp, '%d/%m/%Y') >= Str_To_Date('" . $_GET['dataIni'] . "', '%d/%m/%Y') ";
   }

   if($_GET['dataFim'] != null) {
      $sWhere .= "          And Str_To_Date(Des.Data_Desp, '%d/%m/%Y') <= Str_To_Date('" . $_GET['dataFim'] . "', '%d/%m/%Y') ";
   }

   if($tipo == 'A') {
     $sSQL  = " Select Pac.Nome, ";
     $sSQL .= "	 	     date_format(str_to_date(Des.Data_Desp, '%d/%m/%Y'), '%d/%m/%Y') Data, ";
     $sSQL .= "		     Tde.Descricao Despesa, ";
     $sSQL .= "		     Est.Nome_Fantasia Estabelecimento, ";
     $sSQL .= "		     Des.Valor ";
     $sSQL .= " From 	 Paciente Pac, ";
     $sSQL .= "		     Despesa Des, ";
     $sSQL .= "		     Estabelecimento Est, ";
     $sSQL .= "		     Tipo_Despesa Tde ";
     $sSQL .= $sWhere;
   }
   else if($tipo == 'S') {
     $sSQL  = " Select     Tde.Descricao Despesa, ";
     $sSQL .= "		         Sum(Des.Valor) Valor ";
     $sSQL .= " From 	     Paciente Pac, ";
     $sSQL .= "		         Despesa Des, ";
     $sSQL .= "		         Estabelecimento Est, ";
     $sSQL .= "    		     Tipo_Despesa Tde ";
     $sSQL .= $sWhere;
     $sSQL .= " Group By   Tde.Descricao ";
   }

   $rs = mysql_query($sSQL);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML>
  <HEAD>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <?php if($tipo == 'A') { ?>
    <TITLE>Relat&oacute;rio Anal&aacute;tico</TITLE>
 <?php } else { ?>
    <TITLE>Relat&oacute;rio Sint&eacute;tico</TITLE>
 <?php } ?>
    <LINK REL="stylesheet" TYPE="text/css" HREF="relatorio.css">
    <STYLE TYPE="text/css">
      P.breakhere { page-break-before:always;border:0px;margin:0px;background:transparent; }
    </STYLE>
  </HEAD>
  <BODY BGCOLOR="#FFFFFF">
     <TABLE id="pg2" ALIGN="CENTER" BORDER="1" CELLPADDING="5" CELLSPACING="0">
        <TR>
           <TD ALIGN="CENTER" COLSPAN="5">
              <SPAN CLASS="BOLD">Liga Feminina de Combate ao C&acirc;ncer de Vera Cruz</SPAN>
           </TD>
       </TR>
 <?php if($tipo == 'A') {
        if (mysql_num_rows($rs) > 0) { ?>
            <TR>
                <TD ALIGN="CENTER"><SPAN CLASS="BOLD">Nome</SPAN></TD>
                <TD ALIGN="CENTER"><SPAN CLASS="BOLD">Data</SPAN></TD>

                <TD ALIGN="CENTER"><SPAN CLASS="BOLD">Despesa</SPAN></TD>
                <TD ALIGN="CENTER"><SPAN CLASS="BOLD">Estabelecimento</SPAN></TD>
                <TD ALIGN="CENTER"><SPAN CLASS="BOLD">Valor</SPAN></TD>
            </TR>
<?php while($fields = mysql_fetch_array($rs)) { ?>
            <TR>
                <TD ALIGN="LEFT"><?= $fields['Nome'] ?></TD>
                <TD ALIGN="CENTER"><?= $fields['Data'] ?></TD>
                <TD ALIGN="LEFT"><?= $fields['Despesa'] ?></TD>
                <TD ALIGN="LEFT"><?= $fields['Estabelecimento'] ?></TD>
                <TD ALIGN="RIGHT"><?= number_format($fields['Valor'], 2, ",", ".") ?></TD>
            </TR>
            <?php $valor += $fields['Valor'] ?>
<?php } ?>
            <TR>
                <TD ALIGN="RIGHT" COLSPAN="4"><SPAN CLASS="BOLD">Total</SPAN></TD>
                <TD ALIGN="RIGHT"><SPAN CLASS="BOLD"><?= number_format($valor, 2, ",", ".") ?></SPAN></TD>
            </TR>
       <?php } else { echo "Nenhum dado encontrado!"; } 
 } else { 
        if (mysql_num_rows($rs) > 0) { ?>
            <TR>
                <TD ALIGN="CENTER"><SPAN CLASS="BOLD">Despesa</SPAN></TD>
                <TD ALIGN="CENTER"><SPAN CLASS="BOLD">Valor</SPAN></TD>
            </TR>
<?php while($fields = mysql_fetch_array($rs)) { ?>
            <TR>
                <TD ALIGN="LEFT"><?= $fields['Despesa'] ?></TD>
                <TD ALIGN="RIGHT"><?= number_format($fields['Valor'], 2, ",", ".") ?></TD>
            </TR>
            <?php $valor += $fields['Valor'] ?>
<?php } ?>
            <TR>
                <TD ALIGN="RIGHT" COLSPAN="1"><SPAN CLASS="BOLD">Total</SPAN></TD>
                <TD ALIGN="RIGHT"><SPAN CLASS="BOLD"><?= number_format($valor, 2, ",", ".") ?></SPAN></TD>
            </TR>
<?php } else { echo "Nenhum dado encontrado!"; } }?>
    </TABLE>
  </BODY>
</HTML>